package com.zhang.sdk.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.zhang.sdk.R;
import com.zhang.sdk.ui.dialog.loading.LoadingUIHelper;
import com.zhang.sdk.utils.ui.ToastUtil;

/**
 * 基础activity页面
 *
 * 功能：包括loading弹出框、toast等方法
 * @author zhang.x
 */
@SuppressLint("Registered")
public class BaseActivity extends AppCompatActivity {
    protected String TAG = "BaseActivity";
    protected BaseActivity context;
    protected boolean isDestroy = false;
    protected LoadingUIHelper loadingUIHelper;
    protected ImageView ivLeft, ivRight;
    protected TextView tvTitle, tvRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = this.getClass().getSimpleName();
        context = this;
        loadingUIHelper = new LoadingUIHelper();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loadingUIHelper.hideDialogForLoading();
        loadingUIHelper = null;
        isDestroy = true;
    }

    public void setTitleBarName(String title) {
        setTitleBarName(title, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void setTitleBarName(String title, View.OnClickListener listener) {
        ivLeft = findViewById(R.id.iv_back);
        ivRight = findViewById(R.id.iv_right);
        tvTitle = findViewById(R.id.tv_title);
        tvRight = findViewById(R.id.tv_right);
        if (listener != null) {
            ivLeft.setVisibility(View.VISIBLE);
            ivLeft.setOnClickListener(listener);
        }
        tvTitle.setText(title);
    }

    public void showLoadingDialog(String msg) {
        showLoadingDialog(msg, true);
    }

    public void showLoadingDialog(final String msg, boolean cancelable) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (loadingUIHelper != null) {
                    loadingUIHelper.showDialogForLoading(context, msg, true);
                }
            }
        });

    }

    public void hideLoadingDialog() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (loadingUIHelper != null) {
                    loadingUIHelper.hideDialogForLoading();
                }
            }
        });

    }

    public void showToast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.showLong(context, msg);
            }
        });
    }
}
